﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace EnterpriseFramework.DataAccess
{

    public class Column<T>
    {
        string _propertyName;
        internal string PropertyName
        {
            get { return _propertyName; }
            set { _propertyName = value; }
        }

        public Column(string propertyName) { _propertyName = propertyName; }

        public static Where operator ==(Column<T> v1, T val) { return GenerateReturned(v1, val, "="); }
        public static Where operator !=(Column<T> v1, T val) { return GenerateReturned(v1, val, "!="); }
        public static Where operator >=(Column<T> v1, T val) { return GenerateReturned(v1, val, ">="); }
        public static Where operator >(Column<T> v1, T val) { return GenerateReturned(v1, val, ">"); }
        public static Where operator <=(Column<T> v1, T val) { return GenerateReturned(v1, val, "<="); }
        public static Where operator <(Column<T> v1, T val) { return GenerateReturned(v1, val, "<"); }

        public static implicit operator string(Column<T> d) { return d._propertyName; }

        /// <summary>
        /// Used to sorted in ascending order
        /// </summary>
        public string Ascending
        {
            get { return _propertyName + " ASC"; }
        }

        /// <summary>
        /// Used to sorted in descending order
        /// </summary>
        public string Descending
        {
            get { return _propertyName + " DESC"; }
        }

        public Where Like(string value)
        {
            return new Where(_propertyName + " Like '" + value + "'", null);
        }

        public Where Between(object value1, object value2)
        {
            Random rnumber = new Random();
            string paramName1 = "@" + rnumber.Next();
            string paramName2 = "@" + rnumber.Next();
            Parameter p1 = new Parameter();
            Parameter p2 = new Parameter();

            p1.DbType = p2.DbType = (DbType)Enum.Parse(typeof(DbType), typeof(T).Name);
            p1.ParameterName = paramName1;
            p1.Value = value1;
            p1.Direction = p2.Direction = ParameterDirection.Input;

            p2.ParameterName = paramName2;
            p2.Value = value2;

            return new Where(_propertyName + " BETWEEN " + paramName1 + " AND " + paramName2, p1, p2);
        }

        private static Where GenerateReturned(Column<T> v1, T val, string op)
        {
            Random rnumber = new Random();
            string paramName = "@" + rnumber.Next();
            Parameter p = new Parameter();

            p.DbType = (DbType)Enum.Parse(typeof(DbType), typeof(T).Name);
            p.ParameterName = paramName;
            p.Value = val;
            p.Direction = ParameterDirection.Input;

            return new Where(v1.PropertyName + op + paramName, p);
        }
    }
}
